/*
 * Copyright (c) 2011-2012 Alexander Dubu
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * o  Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * o  Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * o  Neither the name Axil nor the names of its contributors may be used to
 *    endorse or promote products derived from this software without specific
 *    prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

package axil.compiler;

import axil.api.Context;
import axil.api.error.AxilException;
import axil.compiler.ast.Node;
import axil.compiler.lexer.Lexer;
import axil.engine.Config;
import axil.engine.Repository;
import axil.engine.function.Functor;
import axil.framework.error.ApplicationException;
import axil.framework.localization.Namespace;
import axil.vm.Executable;
import axil.vm.opcode.Opcode;

import java.io.Reader;

import static axil.framework.Functions.*;


/**
 * The actual Axil compiler. Axil uses a concept of intelligent AST nodes in
 * which each node performs its own correctness checking, optimization and
 * code generation. Thus, there is not much in the compiler itself. This makes
 * the language extremely flexible, since we mechanisms can be easily added
 * without having to tear the entire thing apart.
 */
public class AxilCompiler {
	public static final Namespace ns = axil().ns("compiler");

	private Config config;
	private Context context;
	private Reader input;
	private String ident;
	private int source;


	public AxilCompiler(String ident, Reader input, boolean register,
	                    Config config, Context context) {
		config.seal();
		this.ident = ident;
		this.input = input;
		this.source = 0;
		this.config = config;
		this.context = context;

		if (register) {
			source = Repository.instance().register(ident);
		}
	}


	public Opcode compile() {
		return compile(Scope.root);
	}


	public Opcode compile(Scope scope) {
		Parser p = new Parser(new Lexer(config, input, source));
		Node node = p.parse();
		node = node.compile(new Compilation(ident, config, context), scope);
		return node.emit();
	}


	/**
	 * Compile the code that this compile points to, returning that code as a
	 * function.
	 */
	public Functor functor() {
		try {
			Parser p = new Parser(new Lexer(config, input, source));
			Node node = p.parse();
			Compilation c = new Compilation(ident, config, context);
			node = node.compile(c, Scope.root);
			return callable(node.emit());
		} catch (AxilException e) {
			throw e;
		} catch(Exception e) {
			throw new ApplicationException(ns, "general-error", e,
			                               nv("script", ident));
		}
	}


	public Executable script() {
		try {
			Parser p = new Parser(new Lexer(config, input, source));
			Node node = p.parse();
			Compilation c = new Compilation(ident, config, context);
			node = node.compile(c, Scope.root);
			Opcode code = node.emit();
			return new Executable(ident, c.references(), code);
		} catch (AxilException e) {
			throw e;
		} catch(Exception e) {
			throw new ApplicationException(ns, "general-error", e,
			                               nv("script", ident));
		}
	}
}
